package com.zyg.user.controller;





import com.alibaba.fastjson.JSONObject;
import com.zyg.common.utils.R;
import com.zyg.user.client.ManagerClient;
import com.zyg.user.entity.TbUser;
import com.zyg.user.service.UserService;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ManagerClient managerClient;
//    @Autowired
//    private RestTemplate restTemplate;

    @GetMapping({"/","register.html"})
    public String index(HttpSession session,Model model){
        //得到session中你的数据
        JSONObject user = (JSONObject) session.getAttribute("user");
        System.out.println("user = " + user);
        //存放到model中
        model.addAttribute("user",user);
        return "register";
    }
    //根据手机号获取验证码
    @GetMapping("/user/getCode")
    public String register(String phone){
        //1、根据手机号生成验证吗
        userService.createCode(phone);

        return "register";
    }

    //添加用户（需要验证码）
    @PostMapping("/user/add")
    public String add(TbUser user,String validcode){
        System.out.println("user = " + user);
        //验证验证码是否存在
        boolean b = userService.isExistsCode(validcode,user.getPhone());
        System.out.println("validcode = " + validcode);
        if (b){
            userService.add(user);
        }
        //应该到登录页面
        return "register";
    }

    //查询zyg-manager微服务的所有品牌
    @GetMapping("manager/brand/list")
    @ResponseBody
    public R findAll(HttpServletRequest request){
        // HttpServletRequest request = ServletActionContext.getRequest();
        //获取用户名（cas登录名的获取方式）
//        AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();
//        String username = principal.getName();
//        System.out.println("username = " + username);
        return managerClient.findAll();
    }
    //根据用户id查询用户
    @GetMapping("/user/findById/{id}")
    @ResponseBody
    public R findById(@PathVariable Long id){
        //根据用户id查询得到用户
        TbUser user = userService.findById(id);
        return R.ok().put("user",user);
    }

    //根据uid查询tb_user表中的用户是否存在
    @GetMapping("/user/findByUid/{uid}")
    @ResponseBody
    public TbUser findByUid(@PathVariable String uid){
        TbUser user = userService.findByUid(uid);
        return userService.findByUid(uid);
    }

    //添加用户(不需要验证码)
    @PostMapping("/user/insert")
    @ResponseBody
    public R insert(@RequestBody TbUser user){
        userService.add(user);
        return R.ok();
    }

    //修改用户(不需要验证码)
    @PostMapping("/user/update")
    @ResponseBody
    public R update(@RequestBody TbUser user){
        userService.update(user);
        return R.ok();
    }
}